/**
 * getFacultyList   获取学部列表
 * queryFacultyList 根据name模糊查询学部
 * addFaculty       添加学部
 * getInfoFaculty   获取学部信息
 * updateFaculty    修改学部信息
 * deleteFaculty    删除学部
 */
const facultyStatement = require("../../common/sql_sentences/faculty")
const classStatement = require("../../common/sql_sentences/class")

exports.getFacultyList = async (req, res) => {
  try {
    if (req.query.name) {
      let { page, pagesize, name } = req.query
      // 根据name模糊查询学部总数
      let total,
        sqlStr = facultyStatement.selectCountLike(name)
      let results = await RunSQL(sqlStr)
      if (!results[0].total) return res.successSend("没有找到相关名称！", 10005)
      total = results[0].total

      // 根据name模糊查询获取学部列表
      sqlStr = facultyStatement.selectLikeLimit(name, page, pagesize)
      results = await RunSQL(sqlStr)
      return res.successSend("", 10000, { row: results, total })
    } else {
      let { page, pagesize } = req.query
      // 获取学部总数
      let total,
        sqlStr = facultyStatement.selectCount()
      let results = await RunSQL(sqlStr)
      total = results[0].total

      if (!pagesize) {
        pagesize = total
      }

      // 获取学部列表
      sqlStr = facultyStatement.selectLimit(page, pagesize)
      results = await RunSQL(sqlStr)
      return res.successSend("", 10000, { row: results, total })

    }
  } catch (error) {
    res.errSend(error)
  }

}

exports.addFaculty = async (req, res) => {
  try {
    const data = req.body
    // 查询学部名称是否重复
    let sqlStr = facultyStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results[0]?.id && results[0]?.id !== data.id) return res.successSend("名称已存在！", 10005)

    // 添加学部
    sqlStr = facultyStatement.insert(req.body)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("添加学部失败！", 10005)
    return res.successSend("添加学部成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoFaculty = async (req, res) => {
  try {
    // 获取学部信息
    let sqlStr = facultyStatement.idSelect(req.body.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateFaculty = async (req, res) => {
  try {
    const data = req.body
    // 查询学部名称是否重复
    let sqlStr = facultyStatement.select({ name: data.name })
    let results = await RunSQL
    if (results[0]?.id && results[0]?.id !== data.id)
      return res.errSend("名称已存在！", 10005)

    // 修改学部信息
    sqlStr = facultyStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改学部信息失败！", 10005)
    return res.successSend("修改学部信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteFaculty = async (req, res) => {
  try {
    const id = req.params.id
    // 获取学部信息
    let sqlStr = facultyStatement.idSelect(id)
    let results = await RunSQL(sqlStr)

    // 查询该学部是否存在班级
    sqlStr = classStatement.select({ faculty: results[0].name })
    results = await RunSQL(sqlStr)
    if (results.length)
      return res.successSend("删除学部失败!有班级属于该学部，请删除班级后重试！", 10005)

    // 删除学部
    sqlStr = facultyStatement.delete(data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("删除学部失败！", 10005)
    return res.successSend("删除学部信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}
